<template>
  <div class="_wz_cent_main _wz_TravelLog">
    <Card dis-hover :bordered="true" style="margin:0 0 10px;">
      <p slot="title">搜索</p>
      <div slot="extra">
        <Button type="info" size="small" v-if="!searchType" @click="searchType = true" icon="ios-arrow-down">更多</Button>
        <Button type="info" size="small" v-else @click="searchType = false" icon="ios-arrow-up">收起</Button>
      </div>
      <Form :model="formDataSearch" label-position="right" :label-width="100" inline>
        <FormItem label="运输企业" v-if="!seeCarData">
          <i-select v-model="formDataSearch.deptUuid" filterable placeholder="请选择运输企业" clearable>
            <Option v-for="item in subDeptList" :value="item.uuid" :key="item.uuid">{{ item.deptName }}</Option>
          </i-select>
        </FormItem>
        <FormItem label="运输车辆">
          <i-input v-model="formDataSearch.vehiclePlateNo" placeholder="请填写运输车辆" clearable />
        </FormItem>
        <FormItem label="从业人员" v-if="searchType">
          <i-input v-model="formDataSearch.driverName" placeholder="请填写从业人员" clearable />
        </FormItem>
        <FormItem label="行车状态" v-if="searchType">
          <i-select v-model="formDataSearch.checkType" filterable placeholder="请选择行车状态" clearable>
            <Option value="1">行车前</Option>
            <Option value="2">行车中</Option>
            <Option value="3">行车后</Option>
          </i-select>
        </FormItem>
        <FormItem label="起运时间" v-if="searchType">
          <DatePicker dataformatas="yyyy-MM-dd" type="date" v-model="formDataSearch.createDate" @on-change='formDataSearch.createDate = $event' placeholder="请选择起运时间"></DatePicker>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="searchSubmit">搜索</Button>
        </FormItem>
      </Form>
    </Card>
    <Card dis-hover :bordered="true">
      <p slot="title">列表</p>
      <p slot="extra" v-if="!seeCarData">
<!--        <Button type="primary" @click="selectData = {};addLogType = true;formTitle = '添加';">添加</Button>-->
        <Button type="success" @click="vehicleExport">导出</Button>
      </p>
      <wzTbale ref="wzTbale" :tableData="tableData"></wzTbale>
    </Card>
    <Drawer width="1100" :mask-closable="false" :draggable="true" :title="formTitle" v-model="addLogType" :styles="formOperationStyles">
      <TravelLogAdd v-if="addLogType" @closeFun="closeFun" :selectData="selectData"></TravelLogAdd>
    </Drawer>

    <Drawer width="1100" :mask-closable="false" :draggable="true" title="详情" v-model="logDetail">
      <TravelLogDetail v-if="logDetail" :currentTravelLogId="currentTravelLogId" />
    </Drawer>

    <Drawer width="1180" :mask-closable="false" :draggable="true" title="查看轨迹" v-model="trajectoryType">
      <trajectory v-if="trajectoryType" :currentTravelLogId="currentTravelLogId"></trajectory>
    </Drawer>


    <Drawer width="850" :mask-closable="false" :draggable="true" title="交接班记录" v-model="successionType">
      <succession v-if="successionType" :selectData="selectData"></succession>
    </Drawer>
  </div>
</template>

<script>
  import TravelLogAdd from "../../../components/safety/operations/travelLog/TravelLogAdd";
  import TravelLogDetail from "../../../components/safety/operations/travelLog/TravelLogDetail";
  import wzTbale from "../../../components/table/table";
  import trajectory from "./trajectory";
  import succession from "./succession";

  export default {
    props: ['seeCarData'],//接收来自父组件的数据
    components: {
      TravelLogAdd,//添加
      TravelLogDetail,//详情
      wzTbale,//列表
      trajectory,
      succession,//交接班
    },
    data() {
      return {
        successionType:false,
        formTitle:'',
        selectData:{},
        trajectoryType:false,
        searchType:false,//搜索条件
        formOperationStyles: {//表单左侧移出css
          height: 'calc(100% - 55px)',
          overflow: 'auto',
          paddingBottom: '53px',
          position: 'static'
        },
        currentTravelLogId: 0,//查看详情id
        addLogType: false,
        logDetail: false,
        subDeptList: [],
        formDataSearch: {
          deptUuid:!!this.seeCarData?this.seeCarData.deptUuid:''
        },//搜索表单数据

        tableData: {
          url: this.apiUrl.webApi + "/safety/drivingLog/drivingLogPageList",//请求地址
          method: 'post',//请求方式
          where: {deptUuid:!!this.seeCarData?this.seeCarData.deptUuid:''},//搜索条件
          height: 'auto',//表格高度 auto
          page: true,//分页是否使用
          columns: [//表头数据
            {type: 'index', width: 70, align: 'center', title: '序号'},
            {title: '登记编号', minWidth: 160, key: 'logRecordNumber'},
            {
              title: '出车状态', key: 'beforeConclusion', minWidth: 140, render: (h, params) => {
                //（1：低风险，2：一般风险，3：较大风险:，4：重大风险） riskItemGrade;
                var color = '', test = '';
                if (params.row.beforeConclusion == 1) {
                  color = 'success', test = '允许出车';
                } else if (params.row.beforeConclusion == 0) {
                  color = 'error', test = '禁止出车';
                }
                return h('Tag', {
                  props: {
                    type: 'dot',
                    color: color
                  }
                }, test);
              }
            },
            {
              title: '行车状态',  minWidth: 140, render: (h, params) => {
                var color = '', test = '';
                if (params.row.checkType == 3) {
                  color = 'success', test = '收车后';
                } else if (params.row.checkType == 2) {
                  color = 'warning', test = '行车中';
                } else if (params.row.checkType == 1) {
                  color = 'primary', test = '出车前';
                }else{
                  color = '', test = '未开始';
                }
                return h('Tag', {props: {type: 'dot',color: color}}, test);
              }
            },
            {title: '运输车辆', key: 'vehiclePlateNo', minWidth: 110},
            {title: '从业人员', key: 'driverName', minWidth: 100},
            {title: '联系电话', key: 'driverPhone', minWidth: 120},
            {title: '天气情况', key: 'weather', minWidth: 100},
            {title: '运输性质', key: 'goods', minWidth: 100},
            {title: '发车时间', key: 'beginDate', minWidth: 160},
            {title: '收车时间', key: 'endDate', minWidth: 160},
            {title: '运输企业', minWidth: 260, key: 'deptName'},
            {
              title: '操作', fixed: 'right', width: 120,
              render: (h, params) => {
                let operationButtons = [];
                operationButtons.push(h('span', {
                  style: {"color": "#2d8cf0", "cursor": "pointer"},
                  on: {
                    click: () => {
                      this.currentTravelLogId = params.row.uuid
                      this.logDetail = true
                    }
                  }
                }, '查看'))

                var buttonList = [];

                // buttonList.push(h('DropdownItem', {
                //   nativeOn: {
                //     click: () => {
                //       this.selectData = JSON.parse(JSON.stringify(params.row));
                //       this.successionType = true;
                //     }
                //   }
                // }, '交接班'))
                buttonList.push(h('DropdownItem', {
                  nativeOn: {
                    click: () => {
                      this.currentTravelLogId = params.row.uuid
                      this.trajectoryType = true;
                    }
                  }
                }, '轨迹'))


                //
                // if (params.row.checkType == 3) {
                //   buttonList.push(h('DropdownItem', {
                //     nativeOn: {
                //       click: () => {
                //         this.formTitle = '修改';
                //         this.selectData = JSON.parse(JSON.stringify(params.row));
                //         this.addLogType = true;
                //       }
                //     }
                //   }, '修改'))
                // }
                buttonList.push(h('DropdownItem', {
                  nativeOn: {
                    click: () => {
                      this.deleteTravelLogById(params.row);
                    }
                  }
                }, '删除'))

                if (buttonList.length > 0 && !this.seeCarData) {
                  if (operationButtons.length > 0) {
                    operationButtons.push(h('Divider', {props: {type: 'vertical',}}, ''))
                  }
                  operationButtons.push(h('Dropdown', {props: {trigger: "hover", transfer: true}},
                    [
                      h('span', {style: {"color": "#2d8cf0", "cursor": "pointer"}}, '更多▼'),
                      h('DropdownMenu', {slot: "list"}, buttonList)
                    ])
                  )
                }

                return operationButtons;
              }
            }
          ],
          format: {//返回数据格式
            list: ['data'],//数据list
            count: ['count'],//数据总量
          },
        },
      }
    },
    mounted() {//mounted 挂载结束状态
      var that = this;


      /*加载子企业列表*/
      that.axios.post(that.apiUrl.coreDeptCoreDeptList, {}).then(res => {
        if (!!res) {
          that.subDeptList = res.data.data;
        }
      }).catch(err => {
        console.log("失败", err)
      })


    },
    methods: {//执行的方法
      searchSubmit() {//搜索
        this.tableData.where = JSON.parse(JSON.stringify(this.formDataSearch));
        this.$refs.wzTbale.tableLoadData(true);
      },
      //删除
      deleteTravelLogById(data) {
        var that = this;
        that.$Modal.confirm({
          title: '删除提醒',
          content: '确定要删除该条记录吗？',
          onOk: () => {
            that.axios.post(that.apiUrl.webApi + "/safety/drivingLog/drivingLogDelete", {uuid: data.uuid}).then(res => {
              if (res) {
                that.$Message.success('删除成功')
                that.$refs.wzTbale.tableLoadData();
              }
            }).catch(err => {
              console.log("失败", err)
            })
          }
        })
      },

      //父级关闭
      closeFun(data) {
        var that = this;
        if (data) {//添加修改 刷新表单
          that.$refs.wzTbale.tableLoadData();
        }
        that.addLogType = false;

      },
      //车辆导出
      vehicleExport() {
        let that = this;
        that.spinShow = true;
        that.axios.post(that.apiUrl.webApi + "/safety/drivingLog/drivingLogExport",  that.formDataSearch).then(res => {
          that.spinShow = false;
          if (!!res) {
            window.open(that.apiUrl.imgUrl + res.data.data)
          }
        }).catch(err => {
          console.log('失败', err);
        })
      },
    },
    watch: {//监听

    },
    beforeUpdate() {//beforeUpdate 更新前状态

    },
    updated() {//updated 更新完成状态

    },
    beforeDestroy() {//beforeDestroy 销毁前状态

    },
    destroyed() {//destroyed 销毁完成状态

    }
  }
</script>

<style lang="less">
  ._wz_TravelLog {
    display: flex;
    flex-direction: column;


  }
</style>
